package com.xlh.wjdcserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.property.dto.PermissionAddDTO;
import com.property.dto.RoleAddDTO;
import com.property.dto.RoleEditDTO;
import com.property.dto.RolePageDTO;
import com.property.entity.Permission;
import com.property.entity.Role;
import com.property.vo.PermissionVO;
import com.xlh.wjdcserver.mapper.RoleMapper;
import com.xlh.wjdcserver.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public void add(RoleAddDTO roleAddDTO) {
        Role role = new Role();
        BeanUtil.copyProperties(roleAddDTO, role);
        baseMapper.insert(role);
    }

    @Override
    public Page<Role> rolePage(RolePageDTO rolePageDTO) {
        Page<Role> page = new Page<>(rolePageDTO.getCurrent(), rolePageDTO.getSize());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getDeleteFlag, "0");
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<Permission> permissionList() {
        return baseMapper.permissionList();
    }

    @Override
    @Transactional
    public void addPermission(PermissionAddDTO permissionAddDTO) {
        // 删除角色权限关系
        baseMapper.deletePermission(permissionAddDTO.getRoleId());

        // 添加角色权限关系
        baseMapper.addPermission(permissionAddDTO);
    }

    @Override
    public List<PermissionVO> getPerByRoleId(String roleId) {
        return baseMapper.getPerByRoleId(roleId);
    }

    @Override
    public void edit(RoleEditDTO roleEditDTO) {
        Role role = new Role();
        BeanUtil.copyProperties(roleEditDTO, role);
        baseMapper.updateById(role);
    }
}
